package icselab.appmodule.service;

import icselab.appmodule.server.ServerManager;
import icselab.dbaccess.DeployDAO;


public class DeploymentChecker extends Thread{ 
	
	private static final String UNDEPLOYED = "undeployed";
	private static final String DONE = "done";
	
	private String deployURL;
	private String instanceID;
	
	DeploymentChecker(String instanceID,String deployURL){
		this.instanceID = instanceID;
		this.deployURL = deployURL;
	}

	public void run(){
		 //check deployment status for 10 times
        ServerManager smg = new ServerManager();
        System.out.println("check instance URL" + deployURL);
		synchronized(this){
		for(int i=1;i<=10;i++)
		{
			try{
				sleep(i*1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        if (smg.isInstanceDeployed(deployURL)){ 
	        	//record in database
	        	DeployDAO dda = new DeployDAO();
	        	dda.updateDeployInfo(instanceID, DONE);
	        	System.out.println("deployed successfully");
	        	return;
	        }
		}
		System.out.println("deployment unfinished");
		}
	}
}
